#include "widget.h"
#include "ui_widget.h"
#include "includes.h"

control *c;
calibration *ca;
static MotorSet *ms;
static About *about;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    c   =  new control;
    ca  =  new calibration;
    ms  =  new MotorSet;
    about= new About;

    /* 添加界面 */
    ui->tabWidget_main->addTab(c, "控制-Control");
    ui->tabWidget_main->addTab(ca, "参数-Parameter");
    ui->tabWidget_main->addTab(ms, "设置-Settings");
    ui->tabWidget_main->addTab(about, "关于-About");

    /* 刷新一次串口列表 */
    uartComm.updata(ui->list_uart);

    this->foc_name = this->ui->label_FocName;
    this->foc_error = this->ui->label_error;

    /* 加载foc图片 */
    mPix.load(":/src/image/地球.png");
    mPix = mPix.scaled(128, 128);

    usb_listener *MUL = new usb_listener;
    qApp->installNativeEventFilter(MUL);
    connect(MUL, &usb_listener::DevicePlugIn, [=](){
        uartComm.updata(ui->list_uart);
        qDebug("DevicePlugIn : do something...");
    });
    connect(MUL, &usb_listener::DevicePlugOut, [=](){
        uartComm.updata(ui->list_uart);
        qDebug("DevicePlugOut : do something...");
    });
    MUL->EmitMySignal();     //手动触发一次
}

void Widget::update_window()
{
    ui->verticalLayoutWidget->setGeometry(QRect(0, 0, this->width(), this->height()));

    c->set_wh(int(this->width()*0.85), int(this->height()*0.9));
    ca->set_wh(int(this->width()*0.83), int(this->height()*0.9));
    ms->set_wh(int(this->width()*0.83), int(this->height()*0.9));
    about->set_wh(int(this->width()*0.83), int(this->height()*0.9));
}

void Widget::paintEvent(QPaintEvent *)
{
    this->update_window();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    uartComm.updata(ui->list_uart);
}

void Widget::on_Button_link_clicked()
{
    uartComm.com = ui->list_uart->currentText();
    uartComm.speed = ui->list_uartSpeed->currentText().toInt();
    uint8_t state = uartComm.Connect(ui->label_link, ui->Button_link);

    QPalette qp;

    if (state == 0) {
        ui->label_FocName->setText("未知设备");
        qp.setColor(QPalette::WindowText,"#43A047");
        ui->label_error->setPalette(qp);
        ui->label_error->setText("无错误");
        uartComm.tx_data(M_GetNume, NULL, 0);
    } else if (state == 1) {
        ui->label_FocName->setText("未连接");
         qp.setColor(QPalette::WindowText,Qt::black);
         ui->label_error->setPalette(qp);
        ui->label_error->setText("未连接");
    } else if (state == 2) {
        ui->label_FocName->setText("未连接");
        qp.setColor(QPalette::WindowText,Qt::black);
        ui->label_error->setPalette(qp);
        ui->label_error->setText("未连接");
    }

    qDebug("确实点击了连接按钮");
    qDebug()<<"串口:" + uartComm.com + "  波特率:" << uartComm.speed;
}

void Widget::SetImageAngle(float angle)
{
    QMatrix matrix;     //定义矩阵

    matrix.rotate(qreal(angle * 360.f));

    QPixmap tmp(mPix.size());

    tmp.fill(Qt::transparent);

    QPainter painter(&tmp);

    painter.translate(mPix.rect().center());

    painter.rotate(qreal(angle * 360.f));

    painter.translate(-mPix.rect().center());

    painter.drawPixmap(mPix.rect(), mPix);

    ui->label_FocImage->setPixmap(tmp);
    ui->label_FocAngle->setText(QString::number(double(angle * 360.f), 'f', 2));
}

void Widget::SetBatAdc(float adc)
{
    ui->Lab_BatAdc->setText(QString::number(double(adc), 'f',2) + "V");
}

void Widget::SetTemp(float adc)
{
    ui->Lab_Temp->setText(QString::number(double(adc), 'f',2) + "°");
}

void Widget::on_Return_Error_clicked()
{
    QPalette qp;

    if (uartComm.tx_uint16(M_ReturnError, 0xfdfd) == Comm_OK) {
        c->CtrDebug("#B71C1C", "错误清除成功,请注意安全!!!");
        qp.setColor(QPalette::WindowText,"#43A047");
        ui->label_error->setPalette(qp);
        ui->label_error->setText("无错误");
   } else {
        c->CtrDebug("#B71C1C", "错误清除失败，请检查连接!!!");
    }
}
